package com.orhon.smartcampus.modules.systemctl.mutation;

import java.util.ArrayList;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.orhon.smartcampus.basicComponents.Result;
import com.orhon.smartcampus.modules.systemctl.dao.OrgDepartmentUserRelationDao;
import com.orhon.smartcampus.modules.systemctl.dao.OrgDepartmentsDao;
import com.orhon.smartcampus.modules.systemctl.dao.OrgDutiesDao;
import com.orhon.smartcampus.modules.systemctl.entity.OrgDepartmentUserRelation;
import com.orhon.smartcampus.modules.systemctl.entity.OrgDepartments;
import com.orhon.smartcampus.modules.systemctl.entity.OrgDuties;
import com.orhon.smartcampus.modules.systemctl.gqlInputModel.GQLDepartmentUsersInput;
import com.orhon.smartcampus.modules.systemctl.gqlModel.GQLDepartmentUsers;
import com.orhon.smartcampus.modules.systemctl.inputMapper.DepartmentUsersInputMapper;
import com.orhon.smartcampus.modules.systemctl.mm.DepartmentUserMapper;
import com.orhon.smartcampus.modules.user.dao.UsersDao;
import com.orhon.smartcampus.modules.user.entity.Users;

import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;

@Component
public class AddSetDepartmentUserRecordMutation implements DataFetcher {

    @Autowired
    OrgDepartmentUserRelationDao orgDepartmentUserRelationDao;

    @Autowired
    UsersDao usersDao;

    @Autowired
    OrgDutiesDao orgDutiesDao;

    @Autowired
    OrgDepartmentsDao orgDepartmentsDao;

    /**
     * 部门分配人员\人员调动
     * editor : ths
     */
    @Override
    public Object get(DataFetchingEnvironment environment) throws Exception {
        GQLDepartmentUsersInput input = DepartmentUsersInputMapper.MAPPER.mapInput(environment.getArgument("inputData"));

        OrgDepartmentUserRelation orgDepartmentUserRelation = null;

        if (input.getRecord_id() != null) {
            orgDepartmentUserRelation = orgDepartmentUserRelationDao.get(OrgDepartmentUserRelation.class, input.getRecord_id());
            orgDepartmentUserRelation.setUpdatedAt(new Date());
        } else {
            orgDepartmentUserRelation = new OrgDepartmentUserRelation();
            orgDepartmentUserRelation.setCreatedAt(new Date());
        }
        if(input.getUser_id() != null) {
            orgDepartmentUserRelation.setUserUsersByUserId(usersDao.get(Users.class, input.getUser_id()));
        }
        if(input.getDuty_id() != null) {
            orgDepartmentUserRelation.setSystemctlOrgDutiesByDutyId(orgDutiesDao.get(OrgDuties.class, input.getDuty_id()));
        }
        if(input.getDepartment_id() != null) {
            orgDepartmentUserRelation.setSystemctlOrgDepartmentsByDepartmentId(orgDepartmentsDao.get(OrgDepartments.class, input.getDepartment_id()));
        }
        if(input.getIs_leaders() != null){

            orgDepartmentUserRelation.setIsLeaders(input.getIs_leaders());
        }
        if(input.getLeave_at() != null){

            orgDepartmentUserRelation.setLeaveAt(input.getLeave_at());
        }
        try{
            orgDepartmentUserRelationDao.saveOrUpdate(orgDepartmentUserRelation);
        }
        catch (Exception e){
            return Result.error(500,""+e);
        }
        ArrayList<GQLDepartmentUsers> list = new ArrayList<GQLDepartmentUsers>();
        GQLDepartmentUsers departmentUsers = DepartmentUserMapper.MAPPER.toOrgDepartmentUserRelation(orgDepartmentUserRelation);
        list.add(departmentUsers);
        return Result.ok().put("data", list);
    }
}

